package com.rerun.phone.memberuser;

import com.baomidou.mybatisplus.plugins.Page;
import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.CollectionJob;
import com.rerun.entity.vo.JobDetailVo;
import com.rerun.enums.CollectionJobStatus;
import com.rerun.service.ICollectionJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * 收藏职位
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /10/26 9:26
 */
@Api(value = "求职端-简历收藏", description = "求职端-简历收藏")
@RestController
@RequestMapping("/memberApi/collectionJob")
public class CollectionJobController extends SuperController {
    @Resource
    private ICollectionJobService iCollectionJobService;


    /**
     * 获取收藏详情状态.
     *
     * @param jobId    the job id
     * @param memberId the member id
     * @return the collection job
     */
    @ApiOperation(value = "获取收藏详情状态", httpMethod = "POST", response = Rest.class, notes = "传入jobId，memberId 获取收藏详情状态")
    @RequestMapping(value = "/get/{jobId}/{memberId}", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest getCollectionJob(@PathVariable("jobId") String jobId, @PathVariable("memberId") String memberId) {
        return Rest.okData(iCollectionJobService.selectByJobId(jobId, memberId));
    }

    /**
     * 收藏/取消收藏.
     *
     * @param jobId            the job id
     * @param memberId         the member id
     * @param collectionStatus the collection status  0 取消收藏   1收藏
     * @return the rest
     * @see CollectionJobStatus
     */
    @ApiOperation(value = "收藏/取消收藏", httpMethod = "POST", response = Rest.class, notes = "collectionStatus 传入 0 取消收藏,1收藏")
    @RequestMapping(value = "/collectOperation", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest add(String jobId, String memberId, int collectionStatus) {

        CollectionJob collection = iCollectionJobService.selectByJobId(jobId, memberId);
//            已经有记录 更新
        if (Objects.nonNull(collection)) {
            updateStatus(collection, collectionStatus);
        } else {
            collectionJob(jobId, memberId, collectionStatus);
        }
        return Rest.ok();
    }

    /**
     * List rest.
     *
     * @param memberId   the member id
     * @param pageNumber the page number
     * @param pageSize   the page size
     * @return the rest
     */
    @ApiOperation(value = "我的职位收藏列表", httpMethod = "POST", response = Rest.class, notes = "带分页 默认每页15条")
    @RequestMapping(value = "/list/{memberId}/{pageNumber}", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest list(@PathVariable("memberId") String memberId, @PathVariable("pageNumber") Integer pageNumber, @RequestParam(defaultValue = "15") Integer pageSize) {
        Page<JobDetailVo> jobPage = getPage(pageNumber, pageSize);
        return Rest.okData(iCollectionJobService.findPageByMemberId(jobPage, memberId));
    }

    private void collectionJob(String jobId, String memberId, int status) {
        CollectionJob collectionJob = new CollectionJob();
        collectionJob.setMemberId(memberId);
        collectionJob.setJobId(jobId);
        collectionJob.setCollectionStatus(status);
        collectionJob.setCreateTime(new Date());
        iCollectionJobService.insert(collectionJob);
    }

    private void updateStatus(CollectionJob job, int status) {
        job.setCollectionStatus(status);
        iCollectionJobService.updateById(job);
    }


}
